package com.dgut.staffmodule.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.dgut.common.util.Gender;
import com.dgut.staffmodule.dao.DepartmentRepository;

public class StaffQueryDTO {
	
	private String staffName;// 项目
	private Gender sex;// 性别
	private String education;// 学历
	// private Date inTime;
	private String deptName;// 部门
	private String position;// 职位
	public String getStaffName() {
		return staffName;
	}

	public void setStaffName(String staffName) {
		this.staffName = staffName;
	}

	public Gender getSex() {
		return sex;
	}

	public void setSex(Gender sex) {
		this.sex = sex;
	}

	public String getEducation() {
		return education;
	}

	public void setEducation(String education) {
		this.education = education;
	}

	/*
	 * public Date getInTime() { return inTime; } public void setInTime(Date inTime)
	 * { this.inTime = inTime; }
	 */
	public String getDeptName() {
		return deptName;
	}

	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}

	public String getPosition() {
		return position;
	}

	public void setPosition(String position) {
		this.position = position;
	}

	public static Specification<Staff> getSpecification(StaffQueryDTO staffQueryDTO) {
		Specification<Staff> spec = new Specification<Staff>() {
			public Predicate toPredicate(Root<Staff> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// 1.Predicate查询条件集合
				List<Predicate> list = new ArrayList<Predicate>();
				
				// 2.根据QueryDTO数据字段的值进行判读那以及条件的组装
				// 姓名
				if (staffQueryDTO != null && !StringUtils.isEmpty(staffQueryDTO.getStaffName())) {
					Predicate p1 = cb.like(root.get("staffName").as(String.class),
							"%" + staffQueryDTO.getStaffName() + "%");
					list.add(p1);
				}
				// 性别
				if (staffQueryDTO != null && !StringUtils.isEmpty(staffQueryDTO.getSex())) {
					Predicate p2 = cb.like(root.get("sex").as(String.class), "%" + staffQueryDTO.getSex() + "%");
					list.add(p2);
				}
				// 学历
				if (staffQueryDTO != null && !StringUtils.isEmpty(staffQueryDTO.getEducation())) {
					Predicate p3 = cb.like(root.get("education").as(String.class),
							"%" + staffQueryDTO.getEducation() + "%");
					list.add(p3);
				}
				// 部门
				if (staffQueryDTO != null && !StringUtils.isEmpty(staffQueryDTO.getDeptName())) {
					Predicate p4 = cb.like(root.get("deptName").as(String.class),
							"%" + staffQueryDTO.getDeptName() + "%");
					list.add(p4);
				}
				// 职位
				if (staffQueryDTO != null && !StringUtils.isEmpty(staffQueryDTO.getPosition())) {
					Predicate p5 = cb.like(root.get("position").as(String.class),
							"%" + staffQueryDTO.getPosition() + "%");
					list.add(p5);
				}
				// 3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				Predicate[] p = new Predicate[list.size()];
				// 4.CriteriaBuilder的and 函数组装 查询条件数组
				return cb.and(list.toArray(p));
			}

		};
		return spec;
	}

}